package imse.team0.scalability.configuration;

import imse.team0.scalability.services.IESBLocalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eugen, 5/3/12 7:02 PM
 */
public class RouteConfigurationProperties implements IRouteConfigurationProperties{

    private static final String CMD_DELIM = ":";
    private static final String cmd_GET_ROUTE_PROPERTIES = "getRouteProperties";
    private static final Logger LOG = LoggerFactory.getLogger(RouteConfigurationProperties.class);

    private static Map<String, Properties> routesProperties = new HashMap<String, Properties>();

    private Properties routeProperties = null;
    private int localServicePort;
    private String routeID;
    private IESBLocalService scalabilityService;

    public void setServicePort(int portNum){
        localServicePort = portNum;
    }

    public void setScalabilityService(IESBLocalService service) {
        scalabilityService = service;
    }

    public void setRouteID(String routeID){
        this.routeID = routeID;
        /*no route was previously installed with this id*/
        if (routesProperties.get(routeID) == null){
            synchronized (RouteConfigurationProperties.class){
                if (routesProperties.get(routeID) == null) {
                    routeProperties = scalabilityService.getRouteProperties(routeID);
                    routesProperties.put(routeID, routeProperties);
                }
            }
        }
        else {
            routeProperties = routesProperties.get(routeID);
        }
    }

    public String getProperty(String propName) {
        return routeProperties.getProperty(propName);
    }

/*    public void storeProperties(Exchange exchange){
        LOG.debug("-------> Received configuration properties for route ("+exchange.getFromRouteId()+")");
        String configurationProperties = exchange.getIn().getBody(String.class);
        String[] properties = configurationProperties.split("\n");
        LOG.debug("-------> Received "+properties.length+" properties");
        for (String property : properties){
            String[] prop = property.split("=");
            routeProperties.put(prop[0], prop[1].trim());
            LOG.debug("-------> Stored property ("+prop[0]+") with value = "+prop[1]);
        }
    }*/

/*    public void getRouteID (Exchange exchange) {
        String command = cmd_GET_ROUTE_PROPERTIES+CMD_DELIM+exchange.getFromRouteId();
        exchange.getIn().setBody(command, String.class);
    }*/
}
